package ai.zixing.memento02;

import lombok.Getter;

import java.util.ArrayList;
import java.util.Random;

/**
 * 玩家类
 */
public class Player {

    @Getter
    private int money;

    // 玩家获得的水果
    private ArrayList<String> fruits = new ArrayList<>();

    private static String[] fruitsName = {"苹果", "葡萄", "香蕉", "橘子"};

    Random random = new Random();

    public Player(int money) {
        this.money = money;
    }

    public String getFruit() {
        String prefix = "";
        if (random.nextBoolean()) {
            prefix = "好吃的";
        }
        //从数组中获取水果
        String f = fruitsName[random.nextInt(fruitsName.length)];
        return prefix + f;
    }

    // 掷骰子游戏
    public void yacht() {
        // 掷骰子
        int dice = random.nextInt(6) + 1;
        if (dice == 1) {
            money += 100;
            System.out.println("所持有的金钱增加了..");
        } else if (dice == 2) {
            money /= 2;
            System.out.println("所持有的金钱减半..");
        } else if (dice == 6) {   //获取水果
            String fruit = getFruit();
            System.out.println("获得了水果: " + fruit);
            fruits.add(fruit);
        } else {
            //骰子结果为3、4、5
            System.out.println("无效数字,继续投掷");
        }
    }

    // 拍摄快照
    public Memento createMemento() {
        Memento memento = new Memento(money);
        for (String fruit : fruits) {
            if (fruit.startsWith("好吃的")) {
                memento.addFruit(fruit);
            }
        }
        return memento;
    }


    //撤销方法
    public void restore(Memento memento){
        this.money = memento.getMoney();
        this.fruits = memento.getFruits();
    }

}